package com.android.paipaiguoji.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.android.paipaiguoji.R;
import com.android.paipaiguoji.activity.mine.BuyTicketActivity;
import com.android.paipaiguoji.activity.mine.DirectBuyPayOrderActivity1;
import com.android.paipaiguoji.utils.ConstantsUrl;
import com.android.paipaiguoji.utils.FuncUtils;
import com.android.paipaiguoji.utils.MyLog;

import org.apache.http.util.EncodingUtils;


/**
 * Wap支付页面
 * @author kristain
 */
public class WapPay extends Activity{

    private WebView        mLoginWebView;
    private ProgressDialog progressDialog;
    private AlertDialog    alertDialog;
    private String         post_data = "";
    private String         form_action = "";
    private String         url_return = "";
    private Handler mHandler = new Handler();

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        post_data = getIntent().getStringExtra(ConstantsUrl.REQ_PARAM);
        form_action = getIntent().getStringExtra(ConstantsUrl.form_action);
        url_return = getIntent().getStringExtra("url_return");


//        MyLog.e("inest","form_action="+form_action);
        if (!FuncUtils.isNull(post_data))
        {
            initWebView(post_data);// 初始化webview
        } else
        {
            Toast.makeText(WapPay.this, "请求参数错误", Toast.LENGTH_LONG).show();
        }
    }

    @SuppressLint("JavascriptInterface")
    private void initWebView(String post_data)
    {
        mLoginWebView = (WebView) findViewById(R.id.loginView);

        WebSettings settings = mLoginWebView.getSettings();
        settings.setSupportZoom(true); //
        settings.setBuiltInZoomControls(true); // 设置支持缩放

        // 加载网页
        mLoginWebView.getSettings().setJavaScriptEnabled(true);
        mLoginWebView.postUrl(form_action, // http://10.10.110.236:81/wap/payment.htm
                EncodingUtils.getBytes(post_data.replace("+", "%2B"), "UTF-8"));

        // 网页加载进度条
        progressDialog = ProgressDialog.show(this, null, "正在加载,请稍后...");
        alertDialog = new AlertDialog.Builder(this).create(); // 创建AlertDialog
        mLoginWebView.setWebViewClient(new MyWebViewClient());
        mLoginWebView.addJavascriptInterface(new Object(){
            public void backTrader(final String str)
            {
                mHandler.post(new Runnable(){
                    public void run()
                    {
                        WapPay.this.finish();
                        if(ConstantsUrl.PAY_SUCCESS.equals(str)){
                            Toast.makeText(WapPay.this,
                                    "支付成功",
                                    Toast.LENGTH_LONG).show();
//                            BaseObject.instance.finish();
//                            BuyTicketActivity.instance.finish();
//                                Intent intent = new Intent(WapPay.this, MainActivity.class);
//                                intent.putExtra("TabSelect",3);
//                                startActivity(intent);
//                                finish();
//                            BaseObject.instance.finish();
//                            BuyTicketActivity.instance.finish();
//                            DirectBuyPayOrderActivity1.instance.finish();
//                            DirectBuyGoodsDetailActivity.instance.finish();
                            if( BaseObject.instance!=null){
//                                Toast.makeText(WapPay.this,"BaseObject.instance.finish();",Toast.LENGTH_LONG).show();
                                BaseObject.instance.finish();

                            }
                            if(  BuyTicketActivity.instance!=null){
                                BuyTicketActivity.instance.finish();

                            }
                            if( DirectBuyPayOrderActivity1.instance!=null){
                                DirectBuyPayOrderActivity1.instance.finish();

                            }
                            if( DirectBuyGoodsDetailActivity.instance!=null){
                                DirectBuyGoodsDetailActivity.instance.finish();

                            }

                        }else if(ConstantsUrl.PAY_FAILURE.equals(str)){
                            Toast.makeText(WapPay.this,
                                    "支付失败",
                                    Toast.LENGTH_LONG).show();
                        }else if(ConstantsUrl.PAY_CANCEL.equals(str)){
                            Toast.makeText(WapPay.this,
                                    "中途取消操作",
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                });
            }
        }, "trader");
    }

    class MyWebViewClient extends WebViewClient{
        /**
         * 拦截URL地址，进行业务操作
         */
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            return false;
        }

        public void onPageStarted(WebView view, String url, Bitmap favicon)
        {
            if(url.contains(url_return)){
//                BaseObject.instance.finish();
//                BuyTicketActivity.instance.finish();
//                DirectBuyPayOrderActivity1.instance.finish();
//                Toast.makeText(WapPay.this,"BaseObject",Toast.LENGTH_LONG).show();
//
//                if( BaseObject.instance==null){
//                    Toast.makeText(WapPay.this,"BaseObject.instance.finish();",Toast.LENGTH_LONG).show();
//                    BaseObject.instance.finish();
//
//                }
                if( BaseObject.instance!=null){
//                    Toast.makeText(WapPay.this,"BaseObject.instance.finish();",Toast.LENGTH_LONG).show();
                    BaseObject.instance.finish();

                }
                if(  BuyTicketActivity.instance!=null){
                    BuyTicketActivity.instance.finish();

                }
                if( DirectBuyPayOrderActivity1.instance!=null){
                    DirectBuyPayOrderActivity1.instance.finish();

                }
                if( DirectBuyGoodsDetailActivity.instance!=null){
                    DirectBuyGoodsDetailActivity.instance.finish();

                }



//                Intent intent = new Intent(WapPay.this, MainActivity.class);
//                intent.putExtra("TabSelect",3);
//                startActivity(intent);
                finish();

            }
//            if (!progressDialog.isShowing()&&!url.contains(url_return))
//            { // 网页开始加载时，显示进度条。
//                progressDialog.show();
//            }



        }

        public void onPageFinished(WebView view, String url)
        {
            if (progressDialog.isShowing())
            { // 加载完毕后，进度条不显示
                progressDialog.dismiss();
            }
        }

        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl)
        {
            Toast.makeText(WapPay.this, "网页加载出错", Toast.LENGTH_LONG).show();
            alertDialog.setTitle("Error");
            alertDialog.setMessage(description);
            alertDialog.setButton("ok", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which)
                {
                }
            });
            alertDialog.show();
        }

        public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                       SslError error)
        {
            handler.proceed(); // 接受所有网站的证书
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
      /*  if ((keyCode == KeyEvent.KEYCODE_BACK) && (mLoginWebView.canGoBack()))
        {
            mLoginWebView.goBack();
            return true;
        }*/
        return super.onKeyDown(keyCode, event);
    }

}
